package com.aqie.demo.npe;

import com.aqie.demo.bean.User;

import java.util.*;

/**
 * @author: aqie
 * @create: 2020-12-27 22:33
 **/
public class EqualOrElse {
    public static void main(String[] args) {
        // test();
        compareToAndEquals();
    }

    private static void test() {
        User user1 = new User("qinyi", 19);
        User user2 = new User("qinyi", 19);
        Set<User> userSet = new HashSet<>();
        userSet.add(user1);
        userSet.add(user2);

        Map<User, Integer> userIntegerMap = new HashMap<>();
        userIntegerMap.put(user1, 0);
        userIntegerMap.put(user2, 0);

        System.out.println(userSet.size());
        System.out.println(userIntegerMap.size());
    }

    private static void compareToAndEquals() {

        List<User> users = new ArrayList<>();
        users.add(new User("qinyi", 10));
        users.add(new User("qinyi", 20));

        User user = new User("qinyi", 20);

        int index1 = users.indexOf(user); // equals
        int index2 = Collections.binarySearch(users, user); // compareTo

        System.out.println(index1);
        System.out.println(index2);
    }
}
